﻿using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using SportWebShop.Data.Entities;

namespace SportWebShop.App_Start
{
	public class NHibernateConfiguration
	{
		private static readonly Configuration configuration = CreateConfiguration();
		private static readonly ISessionFactory sessionFactory = Configuration.BuildSessionFactory();

		private static Configuration CreateConfiguration()
		{
			return Fluently.Configure()
				.Database(MsSqlConfiguration.MsSql2008.ConnectionString(x => x.FromConnectionStringWithKey("sportwebshop")))
				.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Product>())
				.BuildConfiguration();
		}

		public static Configuration Configuration
		{
			get { return configuration; }
		}

		public static ISessionFactory SessionFactory
		{
			get { return sessionFactory; }
		}

		public static void BuildSchema(bool isUpdate)
		{
			if (isUpdate)
			{
				new SchemaUpdate(Configuration).Execute(false, true);
			}
			else
			{
				new SchemaExport(Configuration).Create(true, true);
			}
		}
	}
}